﻿@using FluentValidation
@using System.Text.Json
@using Egroo.UI.Components.Layout;
@using Egroo.UI.Constants;
@using Egroo.UI.Models;
@using jihadkhawaja.chat.client
@using jihadkhawaja.chat.client.Core
@using jihadkhawaja.chat.client.Services

@inject StorageService StorageService
@inject SessionStorage SessionStorage
@inject NavigationManager NavigationManager
@inject IChatAuth ChatAuthService

<MudContainer MaxWidth="MaxWidth.Large" Class="my-10 pt-8">
    <MudIconButton Icon="@Icons.Material.Filled.Home" Href="/" Size="Size.Large" Class="mb-4" />
    <MudText GutterBottom Color="Color.Secondary" Typo="Typo.h6">Sign In</MudText>
    <MudText>Don't have an account? <span><MudButton Href="signup">SIGNUP</MudButton></span></MudText>

    <MudGrid Class="mt-8" Justify="Justify.Center">
        <MudItem xs="12" sm="8" md="6">
            <MudCard Elevation="25" Class="rounded-lg pb-4">
                <MudCardHeader>
                    <CardHeaderContent>
                        <MudText Typo="Typo.h5" Align="Align.Start">Login Account</MudText>
                    </CardHeaderContent>
                </MudCardHeader>
                <MudCardContent>
                    <MudForm @ref="@form" ValidationDelay="0">
                        <MudTextField @bind-Value="@Username"
                                    Immediate="true"
                                    Required="true"
                                    Validation="@(new Func<string, IEnumerable<string>>(NotEmpty))"
                                    Label="Username" />

                        <MudTextField @bind-Value="@Password"
                                    Immediate="true"
                                    Required="true"
                                    Validation="@(new Func<string, IEnumerable<string>>(NotEmpty))"
                                    Label="Password" InputType="InputType.Password" />
                    </MudForm>
                </MudCardContent>
                <MudCardActions Class="d-flex justify-center">
                    <MudButton OnClick="Submit" Disabled=@IsBusy ButtonType="ButtonType.Submit" Variant="Variant.Filled" Color="Color.Primary" Size="Size.Large" Style="width:50%;">SUBMIT</MudButton>
                </MudCardActions>
            </MudCard>
        </MudItem>
    </MudGrid>
</MudContainer>

@code {
    [Inject] ISnackbar Snackbar { get; set; }
    MudForm form;

    string Username { get; set; }
    string Password { get; set; }
    bool IsBusy = false;

    private IEnumerable<string> NotEmpty(string text)
    {
        if (string.IsNullOrWhiteSpace(text))
        {
            yield return "Field is required!";
            yield break;
        }
    }

    private async Task Submit()
    {
        IsBusy = true;

        await form.Validate();

        if (!form.IsValid)
        {
            Snackbar.Add("Please be sure to fill all fields.", MudBlazor.Severity.Error);
            IsBusy = false;
            return;
        }

        dynamic dynamicObj = await ChatAuthService.SignIn(Username, Password);
        Dictionary<string, object> result = null;
        if (dynamicObj is not null)
        {
            result = JsonSerializer.Deserialize<Dictionary<string, object>>(dynamicObj);
        }

        //check user
        if (result is not null)
        {
            Guid Id = Guid.Parse(result["id"].ToString());
            string Token = result["token"].ToString();

            User user = new()
            {
                Id = Id,
            };

            switch (ClientModel.CurrentFrameworkPlatform)
            {
                case FrameworkPlatform.MAUI:
                    StorageService.CreateDirectory(ClientModel.AppDataPath);
                    StorageService.WriteToJsonFile("token", ClientModel.AppDataPath, Token, false, true);
                    break;
                case FrameworkPlatform.WASM:
                case FrameworkPlatform.SERVER:
                    await StorageService.SetLocalStorage("token", Token);
                    break;
                default:
                    throw new Exception("CurrentFrameworkPlatform parameter is undefined.");
            }

            SessionStorage.User = user;

            MobileChatSignalR.Initialize(Source.HubConnectionURL, SessionStorage.Token);
            await MobileChatSignalR.HubConnection.StartAsync();

            NavigationManager.NavigateTo("/channels", true);
        }
        else
        {
            Snackbar.Add("Failed to sign in, Please try again", MudBlazor.Severity.Error);
        }

        IsBusy = false;
    }
}
